package com.pg.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pg.entity.EipTemp;
import com.pg.entity.dd.EipProductionOrder;
import com.pg.entity.dd.EipPurchaseOrder;
import com.pg.entity.dd.EipSalesOrder;
import com.pg.entity.dd.OrderView;
import com.pg.mapper.EipProductionOrderMapper;
import com.pg.mapper.EipPurchaseOrderMapper;
import com.pg.mapper.EipSalesOrderMapper;
import com.pg.mapper.EipTempMapper;
import com.pg.service.EipProductionOrderService;
import com.pg.utils.DataStringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * <p>
 * 生产订单 服务实现类
 * </p>
 *
 * @Author likl
 * @since 2020-10-26
 */
@Service
@DS("proxyDs")
@Primary
public class EipProductionOrderServiceImpl extends ServiceImpl<EipProductionOrderMapper, EipProductionOrder> implements EipProductionOrderService {

    @Autowired
    private EipTempMapper eipTempMapper;

    @Autowired
    private EipPurchaseOrderMapper eipPurchaseOrderMapper;

    @Autowired
    private EipSalesOrderMapper eipSalesOrderMapper;

    @Autowired
    private EipProductionOrderMapper eipProductionOrderMapper;

    @Override
    public void updateProductionOrder(EipTemp eipTemp) {
        LambdaQueryWrapper<EipTemp> eipTempQueryWrapper = new LambdaQueryWrapper<>();
        // 采购订单号
        if (eipTemp.getLinkPUID() != null) {
            eipTempQueryWrapper.eq(EipTemp::getLinkPUID, eipTemp.getLinkPUID());
        }
        EipTemp eipTempByLinkPUID = eipTempMapper.selectOne(eipTempQueryWrapper);
        if (eipTempByLinkPUID == null) {
            eipTemp.setPuid(UUID.randomUUID().toString());
            eipTempMapper.insert(eipTemp);
        } else {
            eipTemp.setPuid(eipTempByLinkPUID.getPuid());
            eipTempMapper.updateByPuid(eipTemp);
        }
        EipProductionOrder eipProductionOrder = new EipProductionOrder();
        eipProductionOrder.setIoflag("mod");
        LambdaQueryWrapper<EipProductionOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EipProductionOrder::getPuid, eipTemp.getLinkPUID());
        eipProductionOrderMapper.update(eipProductionOrder, queryWrapper);
    }

    @Override
    public void logicalDelete(String puid) {
        eipProductionOrderMapper.logicalDelete(puid);
    }

    @Override
    public void logicalDeleteBatch(List<String> asList) {
        for (String puid : asList) {
            eipProductionOrderMapper.logicalDelete(puid);
        }
    }

    @Override
    public EipProductionOrder selectByPuid(String puid) {
        EipProductionOrder eipProductionOrder = eipProductionOrderMapper.selectById(puid);
        eipProductionOrder.setRemark1(selectWarnMessage(eipProductionOrder));
        return eipProductionOrder;
    }

    @Override
    public List<OrderView> selectListByMap(Map<String, String> map) {
        return eipProductionOrderMapper.selectListByMap(map.get("modifyTime"), map.get("ioflag"));
    }

    @Override
    public IPage<EipProductionOrder> selectForList(Page<EipProductionOrder> page, EipProductionOrder eipProductionOrder, Date planStartDateBegin, Date planStartDateFinish) {
        LambdaQueryWrapper<EipProductionOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EipProductionOrder::getIsDeleted, "0");
        //全库唯一码
        if (StringUtils.isNotEmpty(eipProductionOrder.getPuid())) {
            queryWrapper.eq(EipProductionOrder::getPuid, eipProductionOrder.getPuid());
        }
        //生产订单号
        if (StringUtils.isNotEmpty(eipProductionOrder.getIpoNo())) {
            queryWrapper.like(EipProductionOrder::getIpoNo, eipProductionOrder.getIpoNo());
        }
        //销售订单号
        if (StringUtils.isNotEmpty(eipProductionOrder.getPoItemId())) {
            queryWrapper.like(EipProductionOrder::getPoItemId, eipProductionOrder.getPoItemId());
        }
        //种类编码
        if (StringUtils.isNotEmpty(eipProductionOrder.getSubclassCode())) {
            queryWrapper.like(EipProductionOrder::getSubclassCode, eipProductionOrder.getSubclassCode());
        }
        //产品型号
        if (StringUtils.isNotEmpty(eipProductionOrder.getProductModel())) {
            queryWrapper.like(EipProductionOrder::getProductModel, eipProductionOrder.getProductModel());
        }
        //厂家物料编码
        if (StringUtils.isNotEmpty(eipProductionOrder.getMaterialsName())) {
            queryWrapper.like(EipProductionOrder::getMaterialsName, eipProductionOrder.getMaterialsName());
        }
        //计划日期(起)
        if (planStartDateBegin != null) {
            queryWrapper.ge(EipProductionOrder::getPlanStartDate, planStartDateBegin);
        }
        //计划日期(止)
        if (planStartDateFinish != null) {
            queryWrapper.le(EipProductionOrder::getPlanStartDate, planStartDateFinish);
        }
        //母单号
        if (StringUtils.isNotEmpty(eipProductionOrder.getPIpoNo())) {
            queryWrapper.like(EipProductionOrder::getPIpoNo, eipProductionOrder.getPIpoNo());
        }
        //订单状态
        if (StringUtils.isNotEmpty(eipProductionOrder.getIpoStatus())) {
            if ("1".equals(eipProductionOrder.getIpoStatus())) {
                queryWrapper.like(EipProductionOrder::getIpoStatus, eipProductionOrder.getIpoStatus()).or().isNull(EipProductionOrder::getIpoStatus).or().eq(EipProductionOrder::getIpoStatus, "");
            } else {
                queryWrapper.like(EipProductionOrder::getIpoStatus, eipProductionOrder.getIpoStatus());
            }
        }
        if (eipProductionOrder.getPurchaserHqCode() != null) {
            queryWrapper.eq(EipProductionOrder::getPurchaserHqCode, eipProductionOrder.getPurchaserHqCode());
        }
        queryWrapper.orderByDesc(EipProductionOrder::getPlanStartDate);
        queryWrapper.orderByAsc(EipProductionOrder::getIpoNo);
        List<EipProductionOrder> eipProductionOrderListTemp = new ArrayList<>();
        IPage<EipProductionOrder> eipProductionOrderPage = eipProductionOrderMapper.selectPage(page, queryWrapper);
        for (EipProductionOrder eipProductionOrderTemp : eipProductionOrderPage.getRecords()) {

            String simpleName = DataStringUtil.getSimpleName(eipProductionOrderTemp.getMaterialsName());
            eipProductionOrderTemp.setMaterialsName(simpleName);
            eipProductionOrderTemp.setRemark1(selectWarnMessage(eipProductionOrderTemp));
            eipProductionOrderListTemp.add(eipProductionOrderTemp);
        }
        eipProductionOrderPage.setRecords(eipProductionOrderListTemp);
        return eipProductionOrderPage;
    }

    private String selectWarnMessage(EipProductionOrder eipProductionOrder) {
        boolean flag = true;
        if ("1".equals(eipProductionOrder.getDataType())) {
            Map<String, Object> map = new HashMap<String, Object>();
            String poItemId = eipProductionOrder.getPoItemId();
            map.put("\"poItemId\"", poItemId);
            List<EipPurchaseOrder> eipPurchaseOrders = eipPurchaseOrderMapper.selectByMap(map);
            if (!CollectionUtils.isEmpty(eipPurchaseOrders)) {
                flag = false;
            }
        }
        if ("2".equals(eipProductionOrder.getDataType())) {
            String soNo = eipProductionOrder.getPoItemId();
            QueryWrapper<EipSalesOrder> queryWrapper = new QueryWrapper<EipSalesOrder>();
            queryWrapper.eq("\"soNo\"", soNo);
            queryWrapper.eq("\"isDeleted\"", "0");
            Integer num = eipSalesOrderMapper.selectCount(queryWrapper);
            if (num != 0) {
                flag = false;
            }
        }
        String warnMessage = flag ? "10001" : "";
        Date planStartDate = eipProductionOrder.getPlanStartDate();
        Date planFinishDate = eipProductionOrder.getPlanFinishDate();
        Date actualStartDate = eipProductionOrder.getActualStartDate();
        Date actualFinishDate = eipProductionOrder.getActualFinishDate();

        if (planFinishDate != null && actualFinishDate != null) {
            if (planFinishDate.before(actualFinishDate)) {
                warnMessage = "".equals(warnMessage) ? "10003" : warnMessage + ";10003";
            } else if (planFinishDate.after(actualFinishDate)) {
                warnMessage = "".equals(warnMessage) ? "10004" : warnMessage + ";10004";
            } else {
                warnMessage = "".equals(warnMessage) ? "10005" : warnMessage + ";10005";
            }
        }

        if (planStartDate != null && planFinishDate != null && actualStartDate != null && actualFinishDate != null && planStartDate.compareTo(planFinishDate) == 0 && planStartDate.compareTo(actualStartDate) == 0 && actualStartDate.compareTo(actualFinishDate) == 0) {
            warnMessage = "".equals(warnMessage) ? "10002" : warnMessage + ";10002";
        }
        return warnMessage;
    }
}
